package com.chem.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chem.search.entity.ShortPage;
import com.chem.search.esRepository.ESShortPageRepository;
import com.chem.search.mapper.ShortPageMapper;
import com.chem.search.params.ShortPageReq;
import com.chem.search.service.ShortPageService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 权哥
 * @version 1.0
 * @date 2023/9/14 21:58
 */
@Service
public class ShortPageServiceImpl implements ShortPageService {

    @Autowired
    private ShortPageMapper shortPageMapper;
    @Autowired
    private ESShortPageRepository esShortPageRepository;
    @Autowired
    private ElasticsearchRestTemplate restTemplate;
    @Autowired
    private ElasticsearchOperations searchOperations;

    @Override
    public void load() {

        QueryWrapper wrapper = new QueryWrapper();
//        wrapper.le("id",20);
//        wrapper.ge("id",0);

        List<ShortPage> list = shortPageMapper.selectList(wrapper);

        esShortPageRepository.saveAll(list);

    }

    @Override
    public Page<ShortPage> search(String key) {
        Sort sort = Sort.by(Sort.Direction.ASC,"id");
        Pageable pageable = PageRequest.of(1, 10,sort); // 获取第一页，每页10个结果

        return esShortPageRepository.findByField_match(key,pageable);
    }

    @Override
    public Page<ShortPage> searchQuery(ShortPageReq req) {


        Sort sort = Sort.by(Sort.Direction.ASC,"id");
        Pageable pageable = PageRequest.of(req.getPageNumber(), req.getPageSize(),sort); // 获取第一页，每页10个结果

        return esShortPageRepository.findByContentEqualsAndFlag(req.getSearchStr(),req.getFlag(),pageable);

    }

    @Override
    public SearchHits searchOrFlag(ShortPageReq req) {


        return searchByFlags(req);
    }

    public SearchHits searchByFlags(ShortPageReq req) {
        // 构建布尔查询
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        for (String flag : req.getFlags()) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("flag", flag));
        }

        // 如果需要优先显示同时含有多个标签的结果，可以在最后添加一个must子句
      /*  if (req.getFlags().size() > 1) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("flag", req.getFlags().get(0)));
        }*/

        Sort sort = Sort.by(Sort.Direction.ASC,"id");
        Pageable pageable = PageRequest.of(req.getPageNumber(), req.getPageSize(),sort); // 获取第一页，每页10个结果


        // 构建查询对象
        Query query = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(pageable)
                .build();

        // 执行查询操作
        SearchHits searchHits = restTemplate.search(query, ShortPage.class);

        return searchHits;
    }


}
